共计 1700 个字符,预计需要花费 5 分钟才能阅读完成。
在今天使用 Redis 的时候,在 VMware 虚拟机里面安装的 centos 6.5 系统,在其之上安装了 redis 3.2.9,之后使用 java 进行链接操作的时候就出现了一些连接问题,下面就罗列出遇到的问题以及解决办法。
redis.clients.jedis.exceptions.JedisConnectionException: java.net.SocketTimeoutException: connect timed out
出现以上错误,根据提示可以知道是连接超时,是因为 linux 系统没有将 redis 默认端口 6379 进行开放,那么我们只需要打开对应端口即可:
cd /etc
vim sysconfig/iptables
然后在其中插入以下一行内容:
-A INPUT -m state --state NEW -m tcp -p tcp --dport 6379 -j ACCEPT
然后保存,再将服务重启一下即可:
service iptables restart
redis.clients.jedis.exceptions.JedisDataException: DENIED Redis is running in protected mode because protected mode is enabled, no bind address was specified, no authentication password is requested to clients. In this mode connections are only accepted from the loopback interface. If you want to connect from external computers to Redis you may adopt one of the following solutions: 1) Just disable protected mode sending the command ‘CONFIG SET protected-mode no’ from the loopback interface by connecting to Redis from the same host the server is running, however MAKE SURE Redis is not publicly accessible from internet if you do so. Use CONFIG REWRITE to make this change permanent. 2) Alternatively you can just disable the protected mode by editing the Redis configuration file, and setting the protected mode option to ‘no’, and then restarting the server. 3) If you started the server manually just for testing, restart it with the ‘–protected-mode no’ option. 4) Setup a bind address or an authentication password. NOTE: You only need to do one of the above things in order for the server to start accepting connections from the outside.
根据上方的内容阐述大概就是 Redis 运行在保护模式,只允许在本地回路访问,那么我们只需要将保护模式关闭再重启即可:
# 打开 redis.conf
vim redis.conf
#找到 protected-mode yes,将它改为:protected-mode no
#然后保存,退出 vim
#关闭 redis
./bin/redis-cli shutdown
#开启 redis
./bin/redis-server ./redis.conf
在写项目的时候,遇到的奇葩问题千千万,也许是一个很不起眼的错误就让整个系统崩溃,不过只要静下心了,一切都是过眼云烟~